package 
{
	import flash.text.TextFieldAutoSize;
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.display.Bitmap;

	import flash.geom.Rectangle; 

	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class ViewSet 
	{

		public function ViewSet() 
		{

		}

		public static function timeArr2String(s:String,islunar:Boolean=true):String
		{
			/*trace(s);*/
			if(s){
				var arr:Array = s.split("/");
				if(arr.length==4){
					if((arr[0])=="0000")arr[0]="    ";
					if((arr[1])=="00")arr[1]="  ";
					if((arr[2])=="00")arr[2]="  ";
					if(arr[3]=="00")arr[3]="  ";
				}else{
					arr[0]="    ";
					arr[1]="  ";
					arr[2]="  ";
					arr[3]="  ";
				}
			}
			if(islunar)return "农历:"+arr[0]+"年"+arr[1]+"月"+arr[2]+"日 时辰:"+arr[3];
			else return "公历:"+arr[0]+"年"+arr[1]+"月"+arr[2]+"日 时辰:"+arr[3];
		}
		public static function time2toArr(year:String,month:String,day:String,time:String):String
		{
			if(year==null)year="";
			if(month==null)month="";
			if(day==null)day="";
			if(time==null)time="";
			year=year.replace(/[\r\n\s]/g,"");
			month=year.replace(/[\r\n\s]/g,"");
			day=year.replace(/[\r\n\s]/g,"");
			time=year.replace(/[\r\n\s]/g,"");
			if(year.length==0)year="0000";
			if(month.length==0)month="00";
			if(day.length==0)day="00";
			if(time.length==0)time="00";
			return year+"/"+month+"/"+day+"/"+time;
		}
		public static function str2timeArr(s:String):String
		{
			if(s==null)return "0000/00/00/00";
			var str:String = s.replace(/[\r\s\n]/g,"");
			var year:String = s.replace(/[^:]*:([^年]*)年.*/,"$1");
			var month:String = s.replace(/[^年]*年([^月]*)月.*/,"$1");
			var day:String = s.replace(/[^月]*月([^日]*)日.*/,"$1");
			var time:String = s.replace(/.*(时间|时辰):([^:]*)$/,"$2");
			return time2toArr(year,month,day,time);
		}

		public static function date2str(s:String,islunar:Boolean=true,shikaos:String = "失考"):String
		{
			if(s == null)return shikaos;
			var arr:Array = s.split("/");
			if(arr.length != 4)return shikaos;
			var shikao:String = "";
			var datestr:String = islunar?"农历":"公历";
			if(int(arr[0])==0){
				shikao+="年";
			}else{
				datestr += arr[0]+"年";
			}
			if(int(arr[1])==0){
				shikao+="月";
			}else{
				datestr += arr[1]+"月";
			}
			if(int(arr[2])==0){
				shikao+="日";
			}else{
				datestr += arr[2]+"日";
			}
			if((arr[3])=="00"){
				/*shikao+="时辰";*/
			}else{
				datestr += arr[3]+"时";
			}
			if(datestr.length == 2)return shikaos;
			return datestr +" "+((shikao.length > 0)?(shikao +shikaos):"");
		}
		public static function old_itos(_i:int):String
		{
			switch(_i){
				case 0:
					return "";
				case 1:
					return "长";
				case 2:
					return "次";
			}
			var hzstr:String = "零一二三四五六七八九十";
			var wei:String = "个万亿";
			var oldstr:String = String(_i);
			var i:int = 0;
			var len:int = oldstr.length;
			var retstr:String = "";
			while(i<len)
			{
				++i;
				var char:String = oldstr.charAt(len-i);
				if(i%4==1){
					if((i-1)/4 >0) retstr = wei.charAt(i/4)+retstr;
					if(char!="0")retstr = hzstr.charAt(int(char))+retstr;
				}else if(i%4==2){
					if(char == "0"){
						retstr = "零" + retstr;
					}else{
						retstr = hzstr.charAt(int(char))+"十" + retstr;
					}
				}else if(i%4==3){
					if(char == "0"){
						retstr = "零" + retstr;
					}else{
						retstr = hzstr.charAt(int(char))+"百" + retstr;
					}
				} else if(i%4==0){
					if(char == "0"){
						retstr = "零" + retstr;
					}else{
						retstr = hzstr.charAt(int(char))+"千" + retstr;
					}
				}
			}
			return retstr.replace(/[零]*$/g,"").replace(/[零]{1,}/g,"零").replace(/^零*/,"");
		}

		public static function itos(_i:int):String
		{
			if(_i == 0)return "";
			var hzstr:String = "零一二三四五六七八九十";
			var wei:String = "个万亿";
			var oldstr:String = String(_i);
			var i:int = 0;
			var len:int = oldstr.length;
			var retstr:String = "";
			while(i<len)
			{
				++i;
				var char:String = oldstr.charAt(len-i);
				if(i%4==1){
					if((i-1)/4 >0) retstr = wei.charAt(i/4)+retstr;
					if(char!="0")retstr = hzstr.charAt(int(char))+retstr;
				}else if(i%4==2){
					if(char == "0"){
						retstr = "零" + retstr;
					}else{
						retstr = hzstr.charAt(int(char))+"十" + retstr;
					}
				}else if(i%4==3){
					if(char == "0"){
						retstr = "零" + retstr;
					}else{
						retstr = hzstr.charAt(int(char))+"百" + retstr;
					}
				} else if(i%4==0){
					if(char == "0"){
						retstr = "零" + retstr;
					}else{
						retstr = hzstr.charAt(int(char))+"千" + retstr;
					}
				}
			}
			return retstr.replace(/[零]*$/g,"").replace(/[零]{1,}/g,"零").replace(/^零*/,"");
		}

		public static function BoardTitle(str:String):TextField
		{
			var titleTxt:TextField = new TextField();
			titleTxt.defaultTextFormat = new TextFormat("Microsoft YaHei", 14, 0x664422, true);
			titleTxt.autoSize = "left";
			titleTxt.text = (str);
			titleTxt.selectable = false;
			return titleTxt;
		}
		/* -----------------------------------------------*/
		/**
		 * 将_container中的图片处理成平滑 的
		 *
		 * @return  
		 */
		public static function smoothings(_container:DisplayObjectContainer):void
		{

			/*logs.adds(_container is DisplayObjectContainer);*/
			var i:int = 0;
			while(i < _container.numChildren)
			{
				var obj:DisplayObject = _container.getChildAt(i);
				/*logs.adds(_container.getChildAt(i));*/
				var _bmp:Bitmap = obj as Bitmap;
				var shape:Shape= obj as Shape;
				var _container2:DisplayObjectContainer = obj as DisplayObjectContainer;
				if(_bmp)
				{
					/*bmp.pixelSnapping= "auto";*/
					_bmp.smoothing = true;
				}else if(_container2){
					smoothings(_container2);
				}else if (shape){
					/*shape.cacheAsBitmap = true;*/
					_container.removeChild(shape);
					var bmpd:BitmapData = new BitmapData(shape.width/shape.scaleX,shape.height/shape.scaleY,true,0x00000000);
					bmpd.draw(shape);
					var newbmp:Bitmap =new Bitmap(bmpd,"auto",true);
					newbmp.x = shape.x;
					newbmp.y = shape.y;
					newbmp.scaleX = shape.scaleX;
					newbmp.scaleY = shape.scaleY;
					_container.addChildAt(newbmp,i);
				}
				++i;
			}
		}

		public static function maketxtbtn(xx:int, yy:int,str:String, ww:int, hh:int, size:int,clicked:Function,color:uint=0x000000):TextField
		{
			var txt:TextField = new TextField();
			/*txt.border = true;*/
			txt.type = TextFieldType.DYNAMIC;
			txt.x = xx;
			txt.y = yy;
			txt.width = ww;
			txt.height = hh;
			txt.multiline = true;
			txt.wordWrap = true;
			txt.selectable = false;
			var txtformat:TextFormat = new TextFormat("Microsoft YaHei", size,color);
			/*txtformat.leading = size;*/
			txtformat.leftMargin = size;
			txt.defaultTextFormat = txtformat;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.text = (str);
			if(clicked == null){
				txt.mouseEnabled = false;
			}else{
				txt.addEventListener(MouseEvent.CLICK,clicked);
			}
			return txt;
		}
		public static function maketxt(xx:int, yy:int,str:String, ww:int, hh:int, size:int,type:String="dynamic",color:uint = 0x000000):TextField
		{
			var txt:TextField = new TextField();
			/*txt.border = true;*/
			txt.type = type;
			/*txt.type = TextFieldType.INPUT;*/
			txt.x = xx;
			txt.y = yy;
			txt.width = ww;
			txt.height = hh;
			/*txt.multiline = true;*/
			txt.wordWrap = true;
			/*txt.selectable = false;*/
			txt.defaultTextFormat = new TextFormat("Microsoft YaHei", size,color);
			if(str)txt.text = (str);
			return txt;
		}

		public static function setTxt(txt:TextField, xx:int, yy:int, ww:int, hh:int, size:int,type:String, parent:DisplayObjectContainer):void
		{
			txt.border = true;
			txt.type = type;
			txt.x = xx;
			txt.y = yy;
			txt.width = ww;
			txt.height = hh;
			txt.multiline = true;
			txt.wordWrap = true;
			txt.defaultTextFormat = new TextFormat("Microsoft YaHei", size);
			if (parent)
				parent.addChild(txt);
		}

		/**
		 * 在指定的矩形区域内最大化显示
		 * @param	obj
		 * @param	xx
		 * @param	yy
		 * @param	ww
		 * @param	hh
		 */
		public static function fullRectCenter(obj:DisplayObject,rect:Rectangle):void
		{
			fullCenter(obj,rect.x,rect.y,rect.width,rect.height);
		}
		/**
		 * 在指定的矩形区域内最大化显示
		 * @param	obj
		 * @param	xx
		 * @param	yy
		 * @param	ww
		 * @param	hh
		 */
		public static function fullCenter(obj:DisplayObject,xx:int,yy:int,ww:int,hh:int):void
		{
			var scalex:Number = ww / (obj.width/obj.scaleX);
			var scaley:Number = hh / (obj.height/obj.scaleY);
			obj.x = xx;
			obj.y = yy;
			obj.scaleX = obj.scaleY = Math.min(scalex, scaley);
			if (scalex > scaley) obj.x = xx + (ww - obj.width) / 2;
			if (scalex <= scaley) obj.y = yy + (hh - obj.height) / 2;
		}
		/**
		  充满区域，不留边
		  */
		public static function fullRect(obj:DisplayObject,container:DisplayObjectContainer,xx:int,yy:int,ww:int,hh:int):void
		{
			var scalex:Number = ww / (obj.width/obj.scaleX);
			var scaley:Number = hh / (obj.height/obj.scaleY);
			obj.scaleX = obj.scaleY = Math.max(scalex, scaley);
			if (scalex < scaley) obj.x = (ww - obj.width) / 2;
			if (scalex >= scaley) obj.y = (hh - obj.height) / 2;
			var masks:Bitmap = new Bitmap(new BitmapData(ww,hh));
			container.mask = masks;
			container.addChild(obj);
			container.addChild(masks);
			container.x = xx;
			container.y = yy;
		}

		/**
		 * 不缩放，居中显示
		 * @param	obj
		 * @param	xx
		 * @param	yy
		 * @param	ww
		 * @param	hh
		 */
		public static function center(obj:DisplayObject,xx:int,yy:int,ww:int,hh:int):void
		{
			obj.x = xx + ww/2- obj.width/2 ;
			obj.y = yy + hh/2- obj.height/2 ;
		}


		/**
		 * 在遮罩范围内拖动
		 * @param	e
		 */
		public static function drags(e:MouseEvent):void
		{
			var target:Sprite = e.target as Sprite;
			if(target == null)return;
			var targetMask:DisplayObject = target.mask as DisplayObject;
			if(targetMask == null)return;

			if(target.height < targetMask.height)return;
			switch (e.type)
			{
				case MouseEvent.MOUSE_DOWN: 
					target.startDrag(false, new Rectangle(target.x,targetMask.height+ targetMask.y, 0,- targetMask.height - target.height));
					target.addEventListener(MouseEvent.MOUSE_UP, drags);
					target.addEventListener(MouseEvent.MOUSE_OUT, drags);
					target.addEventListener(MouseEvent.MOUSE_MOVE, drags);
					break;
				case MouseEvent.MOUSE_UP: 
				case MouseEvent.MOUSE_OUT: 
					target.stopDrag();
					target.removeEventListener(MouseEvent.MOUSE_UP, drags);
					target.removeEventListener(MouseEvent.MOUSE_OUT, drags);
					target.removeEventListener(MouseEvent.MOUSE_MOVE, drags);
					if( target.y > targetMask.y )
					{
						target.y =targetMask.y;
					}else if( target.y + target.height<targetMask.y+targetMask.height )
					{
						target.y = targetMask.y + targetMask.height - target.height;
					}
					break;
				case MouseEvent.MOUSE_MOVE: 
				default: 
			}

			var bar:Sprite = target.parent["bar"];//拖动条的显示
			/*logs.adds(bar);*/
			if( bar==null )
			{
				return;
			}else {
				var cursor:DisplayObject = bar.getChildAt(bar.numChildren-1);//拖动条
				cursor.height = bar.height * targetMask.height / target.height;
			}
			var rate:Number = (targetMask.y - target.y)/(target.height-targetMask.height);//当前移动的位置【0-1】0为最顶端，1为最低端
			setBar(bar,rate);
		}

		public static function setBar(bar:Sprite,rate:Number):void
		{
			/*logs.adds(bar,rate);*/
			if(rate>1)rate = 1;
			else if (rate < 0) rate = 0;
			var cursor:DisplayObject = bar.getChildAt(bar.numChildren-1);
			cursor.y = (bar.height-cursor.height)*rate;
		}
		public static function make_txt(xx:int, yy:int, _str:String,ww:int=100,hh:int=100, color:uint=0x0,size:uint=16,autoSize:String="left",_wordWrap:Boolean=false,font:String="Microsoft YaHei",type:String=null):TextField
		{
			var _txt:TextField = new TextField();
			if(type)_txt.type = type;
			_txt.width = ww;
			size = Math.min(hh,size);
			if(type == "input"){
				/*_txt.width = ww*2;*/
			}else{
				/*_txt.autoSize = "right";*/
			}
			/*_txt.border = true;*/
			_txt.wordWrap = _wordWrap;
			/*_txt.selectable = false;*/
			_txt.x = xx;
			_txt.y = yy;
			if(!_wordWrap){
				_txt.height = hh+size/2;
				_txt.autoSize = autoSize;
			}else{
				_txt.autoSize = "left";
				_txt.multiline = true;
			}
			_txt.defaultTextFormat = new TextFormat(font, size, color,null,null,null,null,null,autoSize);
			_txt.text = (String(_str));
			return _txt;
		}

		public static function makeParagraph(str:String, ww:int, size:int,color:uint = 0x000000):TextField
		{
			var txt:TextField = new TextField();
			//txt.border = true;
			/*txt.type = TextFieldType.INPUT;*/
			txt.width = ww;
			/*txt.multiline = true;*/
			txt.wordWrap = true;
			/*txt.selectable = false;*/
			txt.defaultTextFormat = new TextFormat("Microsoft YaHei", size,color);
			txt.autoSize = TextFieldAutoSize.LEFT;
			if(str)txt.text = (str);
			return txt;
		}
		public static function makeTxtLine(str:String,size:int=30,color:uint = 0x000000):TextField
		{
			var txt:TextField = new TextField();
			txt.defaultTextFormat = new TextFormat("Microsoft YaHei", size,color);
			txt.autoSize = TextFieldAutoSize.LEFT;
			if(str)txt.text = (str);
			return txt;
		}

		public static function removes(container:DisplayObjectContainer):void
		{
			if(container==null)return;
			var i:int = container.numChildren;
			while (i > 0) 
			{
				--i;
				var obj:DisplayObject = container.getChildAt(i) as DisplayObject;
				if(obj==null)continue;
				if(container.contains(obj))container.removeChild(obj);
				if (obj is Bitmap) Bitmap(obj).bitmapData.dispose();
				if (obj is MovieClip) MovieClip(obj).stop();
				if (obj is Shape) Shape(obj).graphics.clear();
				obj = null;
			}
		}


		public static function clearObj(obj:DisplayObject):void
		{
			removes(obj as DisplayObjectContainer);
			removeObj(obj);
			obj = null;
		}
		public static function removeObj(obj:DisplayObject):void
		{
			if(obj is DisplayObject && obj.parent)
				obj.parent.removeChild(obj);
		}

		/**
		 * 滑块滑动的设定显示位置 
		 * @param bar:Sprite
		 * @return  
		 */
		public static function setPosByBar(bar:DisplayObject,dragTarget:DisplayObject):void
		{
			if(dragTarget.mask.height > dragTarget.height){
				dragTarget.y = dragTarget.mask.y;
				return;
			}
			var rate:Number = -(dragTarget.mask.y - bar.y)/dragTarget.mask.height;
			if(rate>1)rate = 1;
			else if(rate<0)rate=0;
			dragTarget.y = dragTarget.mask.y - (dragTarget.height-dragTarget.mask.height)*rate;
		}

		/**
		 * 设定滑块的位置 
		 * @param bar:Sprite
		 * @return  
		 */
		public static function setBarPos(bar:DisplayObject,dragTarget:DisplayObject):void
		{
			if(dragTarget.mask.height > dragTarget.height){
				bar.y = dragTarget.mask.y;
				return;
			}
			var rate:Number = -(dragTarget.y - dragTarget.mask.y)/(dragTarget.height-dragTarget.mask.height);
			if(rate>1)rate = 1;
			else if(rate<0)rate=0;
			bar.y = dragTarget.mask.y + rate*(dragTarget.mask.height-bar.height);
		}

		/**
		 * 重排容器中的显示对象列表
		 *
		 *
		 *
		 */
		public static function resort(container:DisplayObjectContainer,vert:Boolean=true,gap:int=0,fieldName:Object=null, options:Object = null):void
		{
			if(container==null)return;
			if(fieldName==null)fieldName=["num","InfoID"];
			if(options==null)options = [Array.DESCENDING | Array.NUMERIC,Array.DESCENDING|Array.NUMERIC];
			var arr:Array = new Array;
			var i:int = 0;
			while(i<container.numChildren)
			{
				var obj:DisplayObject = container.getChildAt(i);
				arr.push(obj);
				++i;
			}
			arr.sortOn(fieldName,options);
			i = 0;
			while(i<container.numChildren)
			{
				var curObj:DisplayObject = arr[i];
				container.addChild(curObj);
				if(i==0){
					curObj.x = curObj.y = 0;
				}else if(vert){
					var prevObj:DisplayObject = arr[i-1];
					curObj.y = prevObj.y + prevObj.height;
				}else{
					prevObj = arr[i-1];
					curObj.x = prevObj.x + prevObj.width;
				}
				++i;
			}
		}

		public static function addLine(startx:int=0,endx:int = 720):Shape
		{
			var shape:Shape = new Shape();
			shape.graphics.lineStyle(1,0xff999999);
			shape.graphics.moveTo(startx,0);
			shape.graphics.lineTo(endx,0);
			return shape;
		}

		public static function addBg(parent:DisplayObjectContainer,mc:Sprite=null):Sprite
		{
			if(mc==null)
			{
				mc = new Sprite();
				mc.addChild(new Bitmap(new BitmapData(2,2,true,0)));
			}
			if(parent == mc.parent)
				parent.removeChild(mc);
			mc.width = parent.width;
			mc.mouseChildren = false;
			mc.height = parent.height;
			parent.addChildAt(mc,0);
			return mc;
		}

	}
}

